﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace FID_LearningDataCollection
{
    class CommonUtil
    {
        /// <summary>
        /// 16进制消息转换
        /// </summary>
        /// <param name="message"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Message2Hex(byte[] message, int length)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < length; i++)
            {
                sb.Append(getHexChar((byte)(message[i] >> 4)));
                sb.Append(getHexChar((byte)(message[i] & 0x0F)));
                sb.Append(" ");
            }

            sb.Append("\r\n");
            return sb.ToString();
        }

        private static char getHexChar(byte v)
        {
            switch (v)
            {
                case 0: return '0';
                case 1: return '1';
                case 2: return '2';
                case 3: return '3';
                case 4: return '4';
                case 5: return '5';
                case 6: return '6';
                case 7: return '7';
                case 8: return '8';
                case 9: return '9';
                case 10: return 'A';
                case 11: return 'B';
                case 12: return 'C';
                case 13: return 'D';
                case 14: return 'E';
                case 15: return 'F';
                default: return 'x';
            }
        }
        /// <summary>
        /// 根据读取的消息,分析生成WorkingState类型
        /// </summary>
        /// <param name="GTime"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static WorkingState AnalysisResMessage(DateTime GTime, byte[] message, DateTime lastGTime, float lastP1, float lastP2)
        {
            float P1 = ((Int16)((message[19] << 8) | message[20])) / 100.0f;  //8
            float P2 = ((Int16)((message[21] << 8) | message[22])) / 100.0f;  //9
            //float Freq = ((Int16)((message[43] << 8) | message[44])) / 1.0f;  //20
            float ECurrent = ((Int16)((message[41] << 8) | message[42]));  //19
            float T1 = ((Int16)((message[15] << 8) | message[16])) / 10.0f;  //6
            float T2 = ((Int16)((message[13] << 8) | message[14])) / 10.0f;  //5
            float TEnvir = ((Int16)((message[17] << 8) | message[18])) / 10.0f;  //7

            float superheat = ((Int16)((message[37] << 8) | message[38])) / 10.0f;  //17
            float pidOut = ((Int16)((message[111] << 8) | message[112]));  //54
            float writeOut = ((Int16)((message[129] << 8) | message[130]));  //63
            int limitFlag = ((Int16)((message[169] << 8) | message[170]));  //83
            float iCurrent = ((Int16)((message[39] << 8) | message[40]));  //18
            float p1Set = ((Int16)((message[177] << 8) | message[178]));  //87
            int mainBoardWRStatus = ((Int16)((message[175] << 8) | message[176]));  //86

            int msAfterWrite = ((Int16)((message[183] << 8) | message[184]));  //90
            float stopPressure = ((Int16)((message[179] << 8) | message[180])) / 100.0f;  //88
            float freqMin = ((Int16)((message[185] << 8) | message[186]));  //91
            float freqMax = ((Int16)((message[187] << 8) | message[188]));  //92
            int freqAtMinDelay = ((Int16)((message[189] << 8) | message[190]));  //93

            float FIDFreq = ((Int16)((message[195] << 8) | message[196]));  //96
            //float Freq = ((Int16)((message[197] << 8) | message[198]));  //97
            float Freq = ((Int16)((message[85] << 8) | message[86]));  //41


      return new WorkingState(GTime, P1, P2, Freq, ECurrent, T1, T2, TEnvir, lastGTime, lastP1, lastP2, superheat, pidOut, writeOut, limitFlag, iCurrent,
              p1Set, mainBoardWRStatus, msAfterWrite, stopPressure, freqMin, freqMax, freqAtMinDelay, FIDFreq);
        }

        /// <summary>
        /// 判断当天是否是FID运行
        /// </summary>
        /// <param name="fidDayType"></param>
        /// <param name="hour">切换时间</param>
        /// <returns></returns>
        public static bool isFIDEnable(string fidDayType, int hour)
        {
            int dayOfMonth = DateTime.Today.Day;
            int hourOfDay = DateTime.Now.Hour;
            bool isFid = false;
            if (fidDayType == "双日")
            {
                if (dayOfMonth % 2 == 0 && hourOfDay >= hour) // 双日，切换点之后
                    isFid = true;
                if (dayOfMonth % 2 != 0 && hourOfDay < hour) // 单日，切换点之前
                    isFid = true;
            }
            else
            {
                if (dayOfMonth % 2 != 0 && hourOfDay >= hour) // 单日，切换点之后
                    isFid = true;
                if (dayOfMonth % 2 == 0 && hourOfDay < hour) // 双日，切换点之前
                    isFid = true;
            }

            return isFid;
        }
    }
}
